package com.fengyunhe.manager.web;

import com.fengyunhe.common.bean.PageList;
import com.fengyunhe.common.bean.TreeNode;
import com.fengyunhe.manager.service.AdminRoleService;
import com.fengyunhe.common.AppContext;
import com.fengyunhe.common.bean.Page;
import com.fengyunhe.manager.AdminLog;
import com.fengyunhe.manager.vo.RoleVo;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;

@Controller
@RequestMapping("/admin/role")
public class AdminRoleController extends AdminBaseController {
    static final String MODULE_ID = "manager.role";
    static final String MODULE_NAME = "角色管理";
    @Resource
    private AdminRoleService adminRoleService;

    /**
     * 获取角色名,角色ID
     * @return
     */
    @RequestMapping("/roles.do")
    @ResponseBody
    public List roles(String comb){
        List<Object[]> listObj = new ArrayList<Object[]>();
        List<RoleVo> listVo = adminRoleService.getAllRoleList();
        if(null!=comb){
            listObj.add(new Object[]{"","全部"});
        }
        if(listVo!=null){
            for(RoleVo roleVo:listVo){
                listObj.add(new Object[]{roleVo.getRoleId(),roleVo.getRoleName()});
            }
        }
        return listObj;
    }

    /**
     * 列表
     *
     * @param offset
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    @RequiresPermissions(MODULE_ID + ":view")
    public Map<String, Object> list(Page.Offset offset) {
        Map<String, Object> map = new HashMap<String, Object>();
        PageList<RoleVo> roleList = adminRoleService.getRolePageList(offset);
        map.put("success", true);
        map.put("pageList", roleList);
        return map;
    }

    /**
     * 添加
     *
     * @param role
     * @param perms
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    @RequiresPermissions(MODULE_ID + ":add")
    @AdminLog(module = MODULE_NAME, action = "添加角色")
    public Map<String, Object> add(RoleVo role, String[] perms) {
        Map<String, Object> map = new HashMap<String, Object>();
        adminRoleService.createRole(role, perms);

        map.put("success", true);
        return map;
    }

    /**
     * 查询完整权限树
     *
     * @return
     */
    @RequestMapping("/perm_treenodes")
    @ResponseBody
    @RequiresAuthentication
    public List<TreeNode> permTreeNodes() {
        return AppContext.getModuleManagementPermsTreeNodeList();
    }

    /**
     * 返回角色的权限id
     *
     * @param roleId
     * @return
     */
    @RequestMapping("/perms")
    @ResponseBody
    @RequiresAuthentication
    public Map<String, Object> perms(Integer roleId) {
        Map<String, Object> map = new HashMap<String, Object>();
        Set<String> perms = null;
        if (roleId != null) {
            perms = this.adminRoleService.getPermissionsByRoleId(roleId);
        }
        map.put("success", true);
        map.put("perms", perms);
        return map;
    }


    /**
     * 修改
     *
     * @param role
     * @param perms
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    @RequiresPermissions(MODULE_ID + ":update")
    @AdminLog(module = MODULE_NAME, action = "修改角色")
    public Map<String, Object> update(RoleVo role, String[] perms) {
        Map<String, Object> map = new HashMap<String, Object>();
        boolean updateRole = adminRoleService.updateRole(role, perms);
        map.put("success", updateRole);
        return map;
    }

    /**
     * 删除
     *
     * @param roleId
     * @return
     */
    @RequestMapping("/remove")
    @ResponseBody
    @RequiresPermissions(MODULE_ID + ":remove")
    @AdminLog(module = MODULE_NAME, action = "删除角色")
    public Map<String, Object> remove(Integer roleId) {
        Map<String, Object> map = new HashMap<String, Object>();
        boolean removeRole = adminRoleService.deleteRole(roleId);
        map.put("success", removeRole);
        return map;
    }
}
